package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.Setting;
import com.jumi.microservice.domain.vo.SettingVO;
import com.jumi.microservice.service.SettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "短信配置相关接口")
@RequestMapping("/auth/setting")
public class SettingController {

    @Autowired
    private SettingService settingService;

    @ApiOperation(value = "更新数据", notes = "更新短信配置数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "短信内容签名",value = "mobileSignature"),
            @ApiImplicitParam(name = "短信模板ID", value = "mobileTplid"),
            @ApiImplicitParam(name = "短信模板内容", value = "mobileMemo"),
    })
    @PutMapping("/update")
    public ResponseResult update(@Validated  @RequestBody SettingVO vo, HttpServletRequest request){
        settingService.update(vo,request);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "短信配置", notes = "短信配置数据")
    @GetMapping("/settingList")
    public ResponseResult<List<SettingVO>> settingList(){
        List<Setting> list = settingService.selectList();
        return ResultGenerator.genSuccessResult(pos2Vos(list));
    }

    /**
     * 部门DO对象转换 do->vo
     *
     * @param settings 部门do集合
     * @return 部门VO集合
     */
    private List<SettingVO> pos2Vos(List<Setting> settings) {
        List<SettingVO> pos = settings.stream().map(p -> {
            SettingVO vo = null;
            try {
                vo = p.clone(SettingVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }
}
